import { getInfo, login, logout } from '@/api/login'
import { getToken, removeToken, setToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    roles: [],
    roleType: '',
    teamId: '',
    teamName: '',
    userId: '',
    userName: '',
    projectIds: '',
    isDirector: '',
    unread: '',
    employeeId: '',
    teamIds: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ROLETYPE: (state, roleType) => {
      state.roleType = roleType
    },
    SET_TEAMID: (state, teamId) => {
      state.teamId = teamId
    },
    SET_TEAMNAME: (state, teamName) => {
      state.teamName = teamName
    },
    SET_USEID: (state, userId) => {
      state.userId = userId
    },
    SET_USENAME: (state, userName) => {
      state.userName = userName
    },
    SET_PROJECTIDS: (state, projectIds) => {
      state.projectIds = projectIds
    },
    SET_ISDIRECTOR: (state, isDirector) => {
      state.isDirector = isDirector
    },
    SET_UNREAD: (state, unread) => {
      state.unread = unread
    },
    SET_EMPLOYEEID: (state, employeeId) => {
      state.employeeId = employeeId
    },
    SET_TEAMIDS: (state, teamIds) => {
      state.teamIds = teamIds
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const keyword = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(keyword, userInfo.password).then(response => {
          // const data = response.responseBody
          const data = {
            token: 'admin'
          }
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response.responseBody
          data.roles = ['admin']
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
            commit('SET_NAME', data.userName)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_ROLETYPE', data.roleType)
          commit('SET_TEAMID', data.teamId)
          commit('SET_TEAMNAME', data.teamName)
          commit('SET_USEID', data.userId)
          commit('SET_USENAME', data.userName)
          commit('SET_PROJECTIDS', data.projectIds)
          commit('SET_ISDIRECTOR', data.isDirector)
          commit('SET_UNREAD', data.unread)
          commit('SET_EMPLOYEEID', data.employeeId)
          commit('SET_TEAMIDS', data.teamIds)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    //
    setUnread({ commit }, unread) {
      commit('SET_UNREAD', unread)
    }
  }
}

export default user
